package com.frank.oj.service.admin.problem.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.model.entity.problem.Problem;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.ChangeGroupProblemProgressDTO;
import com.frank.oj.service.admin.problem.AdminGroupProblemService;
import com.frank.oj.service.entity.problem.ProblemEntityService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author frank
 * @Date 2024/4/5
 */
@Service
public class AdminGroupProblemServiceImpl implements AdminGroupProblemService {

    @Resource
    private ProblemEntityService problemEntityService;

    @Override
    public IPage<Problem> getProblemList(Integer currentPage, Integer limit, String keyword, Long gid) {
        if (currentPage == null || currentPage < 1) currentPage = 1;
        if (limit == null || limit < 1) limit = 10;
        IPage<Problem> page = new Page<>(currentPage, limit);

        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "gid", "apply_public_progress", "problem_id", "title", "author", "type", "judge_mode")
                .eq(gid != null, "gid", gid)
                .isNotNull("gid")
                .isNotNull("apply_public_progress")
                .orderByAsc("apply_public_progress", "gid");
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.and(problemQueryWrapper -> problemQueryWrapper.like("title", keyword).or()
                    .like("problem_id", keyword).or()
                    .like("author", keyword));
        }

        return problemEntityService.page(page, queryWrapper);
    }

    @Override
    public void changeProgress(ChangeGroupProblemProgressDTO changeGroupProblemProgressDto) throws StatusFailException {
        Long pid = changeGroupProblemProgressDto.getPid();
        Integer progress = changeGroupProblemProgressDto.getProgress();
        if (pid == null || progress == null) {
            throw new StatusFailException("参数错误，pid或progress不能为空");
        }

        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "is_group", "gid", "apply_public_progress").eq("id", pid);
        Problem problem = problemEntityService.getOne(queryWrapper);

        if (problem == null) {
            throw new StatusFailException("该题目不存在");
        }

        // null为未申请，1为申请中，2为申请通过，3为申请拒绝
        problem.setApplyPublicProgress(progress);
        // todo 存疑
        switch (progress) {
            case 1:
            case 3:
                problem.setIsGroup(true);
                break;
            case 2:
                problem.setIsGroup(false);
                break;
            default:
                throw new StatusFailException("请求参数错误：progress请使用1~3");
        }
        boolean updated = problemEntityService.updateById(problem);
        if (!updated) {
            throw new StatusFailException("修改失败");
        }
    }
}
